<template>
	<view>
		<nav-bar fontColor="#FFFFFF" title="融资申请列表"></nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<v-tabs v-model="activeTab" @change="changeTb" :scroll="false" :bold="true" :tabs="tabs1" field="name"></v-tabs>

		<!-- <uni-data-select  @change="change" style="width: 95%;margin: 0 auto;margin-top: 20rpx;" placeholder="请选择融资客户"
			:localdata="customerList" v-model="customerNo" /></uni-data-select> -->
		<uni-data-select @change="change" style="width: 95%;margin: 0 auto;margin-top: 20rpx;" placeholder="请选择融资产品"
			:localdata="productList" v-model="productNo" /></uni-data-select>


		<view>
			<mescroll-body @init="mescrollInit" @down="downCallback" @up="dataListChoose">
				<view class="orderNumber">数量：{{total2}}个</view>
				<view v-for="(item,index) in dataList2">
					<uni-list>
						<uni-list-item :index="index+1" @click="jumpDetail(item)"
							:title="'产品:' + item.productName + '\n' + '状态：' + (item.status == 'created' ? '待支付服务费' : (item.status == 'fee_paid' ? '正在融资' : (item.status == 'un_submit' ? '正在融资' : (item.status == 'submitted' ? '正在融资' : (item.status == 'paid' ? '已放款' : (item.status == 'rejected' ? '放款失败' : '未知状态')))))) "
							:note="(item.status == 'paid' ? '融资金额:' + item.askFinAmount  + '元' + '\n' + '放款金额:' + item.amount : '融资金额:'+item.askFinAmount)  + '元 \n' + '融资时间：' + item.createTime"
							link="reLaunch" right-text="查看" clickable>
						</uni-list-item>
					</uni-list>
				</view>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	var main = null;
	var receiver = null;
	import uniList from '@/components/uni-list/uni-list.vue';
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	
	let timer=null
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			uniList,
		},
		data() {
			return {
				msgType: 'success',
				messageText: '这是一条成功提示',
				activeTab: 0,
				tbStatus: 0,
				tabs1: [{
						name: '全部',
						value: 1
					},
					{
						name: '待支付服务费',
						value: 2
					},
					{
						name: '融资中',
						value: 3
					},
					{
						name: '已放款',
						value: 4
					}
				],
				//搜索内容
				downOption: {
					native: false,
					use: true, // 是否启用下拉刷新; 默认true
					auto: true
				},
				upOption: {
					use: true, // 是否启用上拉加载; 默认true
					auto: false,
					noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				},
				dataList: [],
				dataList2: [],
				total: null,
				total2: null,
				customerNo: '',
				productNo: '',
				customerList: [],
				productList: [],
				
			};
		},
		onLoad(e) {
			if(e.id){
				console.log(e)
				uni.showLoading({
					title: '加载中...'
				});
				timer = setInterval(()=>{
					this.getPayResult(e.id);
				},2000)
			}
			this.customerSearch();
			this.productSearch();
		},
		onUnload() {
			if(timer != null){
				clearInterval(timer);
				uni.hideLoading();
			}
		},
		
		// onShow() {
		// 	this.mescroll.resetUpScroll();
		// },
		//方法
		methods: {
			getPayResult(e){
				let opt = {
					url:'yeepay/payResult/'+ e,
					method:'GET'
				};
				this.$http.httpTokenRequest(opt).then(res => {
					if(res.data.data == true){
						this.financingApply(e);
						uni.hideLoading();
						console.log('支付成功')
						clearInterval(timer);
					}
					else{
						console.log('支付失败')
					}
				})
			},
			financingApply(e) {
				// 提交融资申请
				let opt = {
					url: 'baoli/finCreditBill/apply/'+ e,
					method: 'POST'
				};
				this.$http.httpTokenRequest(opt,{}).then(res => {
					if (res.data.code == 200) {
						// this.getJdLogin(res.data.data)
						uni.showToast({
							title: res.data.msg,
						});
					} else {
						uni.showToast({
							icon: 'error',
							title: res.data.msg,
						});
					}
				})
			},
			getJdLogin(e){
				uni.navigateTo({
					url:'/pages/login/jdLogin?type=financing&salesOrderId='+e
				})
			},
			// jump(e){
			// 	if(e.proveStatus == 'actived'){
			// 		if(e.productName == '酒易贷'){
			// 			let opt = {
			// 				url:'finance/jd/getJdLoginParams/productUrl/'+ uni.getStorageSync('userInfo').customerId,
			// 				method:'POST'
			// 			};
			// 			this.$http.httpTokenRequest(opt,{}).then(res => {
			// 				uni.navigateTo({
			// 					url:'/pages/login/jdLogin'
			// 				})
			// 			})
			// 		}
			// 	}
			// },
			jumpDetail(e){
				uni.navigateTo({
					url: '/pages/financial-products/financing-apply-details?id='+e.id
				});
				// if(e.status == 'created'){
				// 	this.submit(e.salesOrderNo)
				// }
				// else{
				// 	uni.navigateTo({
				// 		url: '/pages/financial-products/financing-apply-details?id='+e.id
				// 	});
				// }
			},
			productSearch() {
				let that = this;
				let opt = {
					url: 'loanProduct/finLoanProduct/search',
					method: 'POST'
				};
				let params = {
					// status: "created",
					// status: "CREATED"
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						for (let i in res.data.data) {
							that.productList.push({
								text: res.data.data[i].productName,
								value: res.data.data[i].id
							})
						}
					}
				})
			},
			customerSearch() {
				let that = this;
				let opt = {
					url: 'basic/customer/search',
					method: 'POST'
				};
				let params = {
					jdCreditStatus: "ACCESS",
					status: "CREATED"
					// status: "CREATED"
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						for (let i in res.data.data) {
							that.customerList.push({
								text: res.data.data[i].customerName,
								value: res.data.data[i].customerCode
							})
						}
					}
				})
			},
			dataListChoose(e) {
				if (this.tbStatus == 0) {
					const statusList = []
					this.getFinCreditBillList(e,statusList)
				} else if (this.tbStatus == 1) {
					const statusList = ['created']
					this.getFinCreditBillList(e, statusList)
				} else if (this.tbStatus == 2) {
					const statusList = ['fee_paid','un_submit','submitted']
					this.getFinCreditBillList(e, statusList)
				} else if (this.tbStatus == 3) {
					const statusList = ['paid','pushed']
					this.getFinCreditBillList(e, statusList)
				}
			},
			change() {
				setTimeout(()=>{
					this.mescroll.resetUpScroll();
				},200)
				
			},
			changeTb(e) {
				this.tbStatus = e;
				console.log(e)
				this.mescroll.resetUpScroll();
			},
			messageToggle(type, msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open();
			},

			getDataList(page) {
				let that = this;
				let opt = {
					url: 'baoli/finCreditBill/search/' + page.num + '/10',
					method: 'POST'
				};
				let params = {
					// status: "created",
					status: "CREATED"
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 0) {
						this.mescroll.endByPage(res.data.data.rows.length, res.data.data
							.total); //必传参数(当前页的数据个数, 总页数)
						this.total = res.data.data.total
						if (page.num == 1) {
							this.dataList = res.data.data.rows;
						} else {
							this.dataList = this.dataList.concat(res.data.data.rows);
						}
						if (res.data.data.rows == '') {
							// this.messageToggle('error', '暂无数据')
						}
					} else {
						that.mescroll.endErr();
						uni.showToast({
							title: res.data.message,
							icon: 'error',
							duration: 2000
						});
					}
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			getFinCreditBillList(page, statusList) {
				let that = this;
				let opt = {
					url: 'baoli/finCreditBill/search/' + page.num + '/10',
					method: 'POST'
				};
				let params = {
					buyerNo: uni.getStorageSync('userInfo').customerId,
					productNo: that.productNo,
					statusList: statusList
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						that.mescroll.endByPage(res.data.data.records.length, res.data.data
							.total); //必传参数(当前页的数据个数, 总页数)
						that.total2 = res.data.data.total
						if (page.num == 1) {
							that.dataList2 = res.data.data.records;

						} else {
							that.dataList2 = that.dataList2.concat(res.data.data.records);
						}
						if (res.data.data.records == '') {
							// this.messageToggle('error', '暂无数据')
						}
						
					} else {
						
						that.mescroll.endErr();
						uni.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 2000
						});
					}
				}).catch(() => {
					//联网失败, 结束加载
					that.mescroll.endErr();
				})
			},

			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
		},
		//页面下来刷新
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 2000);
		}
	};
</script>
<style lang="scss" scoped>
	page {
		background: #FFFFFF;
	}

	.popup {
		width: 100%;

		view {
			font-size: 35rpx;
			color: #FFFFFF;
			margin-top: 50rpx;
			text-align: center;
			letter-spacing: 3rpx;
			font-weight: 700;
		}

		image {
			display: block;
			margin: 0 auto;
		}
	}

	.end-title {
		display: flex;
		border-bottom: 1px solid #E5E5E5;
	}

	.end-title view {
		flex-grow: 1;
		text-align: center;
		background: #FFFFFF;
		// border-top: 1px solid #E5E5E5;
	}

	.btna {

		border-bottom: 2px solid #007AFF;
		color: #007AFF;
	}

	.tab {
		padding: 30rpx;
		font-size: 35rpx;
	}

	.mapStyle {
		height: 1200rpx;
		width: 100%;
		margin-top: 10rpx;
	}

	/*说明*/
	.orderNumber {
		text-align: center;
		padding: 30rpx;
		font-size: 30rpx;
		letter-spacing: 3rpx;
		font-weight: 700;
		// font-family: '宋体';
		// font-weight: ;
	}

	.imgCode {
		position: relative;
		background: linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) left bottom,
			linear-gradient(#000000, #000000) left bottom;
		background-repeat: no-repeat;
		background-size: 2px 20px, 20px 2px;
		height: 400rpx;
		width: 400rpx;
		margin: 0 auto;
		margin-top: 35%;
	}

	.txtTop {
		margin-top: 50rpx;
	}

	.qrImg {
		width: 250rpx;
		height: 250rpx;
		left: 20%;
		top: 15%;
	}

	.bottomBox {
		position: fixed;
		width: 100%;
		bottom: 0;
		margin-bottom: 20rpx;
		// z-index: 1000;
	}

	.buttonBox {
		width: 95%;
		margin: 0 auto;
		display: flex;
		justify-content: space-around;

		// margin-top: 200rpx;
	}

	.button_fix {
		width: 40%;
		height: 70rpx;
		// margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		font-size: 28rpx;
		line-height: 70rpx;

	}

	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #ff5a5f;
	}

	.slot-button-text {
		color: #ffffff;
		font-size: 14px;
	}
</style>